//
//  WorkHomeVC.m
//  XLDoctor
//
//  Created by longma on 2018/7/27.
//  Copyright © 2018年 XLH. All rights reserved.
//mj语言

#import "LanguageController.h"
#import "LanguageCell.h"
#import "AppDelegate.h"
#import "LoginViewController.h"

@interface LanguageController ()
@property (weak, nonatomic) IBOutlet UIStackView *askStackView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation LanguageController


- (void)viewDidLoad {
    self.backgroundColorType = BackgroundColorTypeGray;
    [super viewDidLoad];
 
    [self requestData];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

}

- (void)mj_setUI{
    [super mj_setUI];
    
    self.mj_titleLocalizable = @"App Language";
    
}

#pragma mark - 方法
- (void)requestData{
    [self.askStackView mj_removeAllSubviews];
    
    NSString *appLanguage = [MJUIUserDeFault objectForKey:MJ_AppLanguage];
  
    NSArray *nameArray = @[@"English",@"Русский",@"Indonesia",@"简体中文",@"繁體中文(港澳)",@"繁體中文(台灣)"];
    NSArray *preferredLanguages = @[PreferredLanguageEN,
                                    PreferredLanguageRU,
                                    PreferredLanguageID,
                                    PreferredLanguageZhHansCN,
                                    PreferredLanguageZhHantCN,
                                    PreferredLanguageZhHantTW,
                                    ];
    self.dataArray = [NSMutableArray array];
    
    MJWeakSelf
    [nameArray enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
        NSString *preferredLanguage = preferredLanguages[idx];
        MJCommonModel *model = [[MJCommonModel alloc]init];
        model.title = obj;
        LanguageCell *cell = [LanguageCell mj_loadNibWithOwner:self];
        if(idx == 0){
            [cell mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(66));
            }];
        }
        cell.model = model;
        cell.iconButton.tag = idx;
        
        [cell.itemButton mj_addAction:^(UIButton *button) {
            
            MJUIAlertViewConfigure *configure = [[MJUIAlertViewConfigure alloc]init];
            configure.confirmButtonColor = UIColor.mj_appColor;
            configure.confirmTitle = MJUILocalizedString(@"Yes");
            configure.cancelTitle = MJUILocalizedString(@"Cancel_qx");
            configure.messageAlignmentCenter = true;
            NSString *messageString = [NSString stringWithFormat:@"%@ %@",MJUILocalizedString(@"Switch language to"),model.title];
            [MJUIAlertView alertViewWithTitle:MJUILocalizedString(@"Switch Language") message:messageString configure:configure confirmBlock:^{
                [LanguageHelper setCurrentLanguage:preferredLanguage];
                [weakSelf updateUI:cell.iconButton];
            } cancelBlock:^{

            }];
            
        }];
        [weakSelf.dataArray addObject:cell.iconButton];
        [weakSelf.askStackView addArrangedSubview:cell];
        
        if([preferredLanguage isEqualToString:appLanguage]){
            [cell.iconButton setSelected:true];
        }
     }];
}

-(void)resetRootViewController{

    AppDelegate * appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    appDelegate.window.rootViewController = nil;

    LoginViewController *loginVC = [[LoginViewController alloc]init];
    BasicNavigationController * nav = [[BasicNavigationController alloc]initWithRootViewController:loginVC];
    [nav pushViewController:[[LanguageController alloc]init] animated:false];
    appDelegate.window.rootViewController = nav;

}

- (void)updateUI:(UIButton *)button {
    
    [self.dataArray enumerateObjectsUsingBlock:^(UIButton *obj, NSUInteger idx, BOOL *stop) {
        [obj setSelected:false];
     }];
    [button setSelected:true];
    
    MJWeakSelf
    MJUIAfter_GCD(0.5, ^{
        [weakSelf.navigationController popToRootViewControllerAnimated:true];
    });
    
}

#pragma mark - 代理

#pragma mark - 懒加载

@end
